﻿using System;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.SystemManage;
using Ninject;
using WinForm.Helper;
using WinForm.Properties;

namespace WinForm.Dialog
{
    public partial class EditSPrivilege : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ISysPrivilege _sysPrivilege = Kernel.Get<ISysPrivilege>();


        public EditSPrivilege()
        {
            InitializeComponent();
        }

        public EditSPrivilege(int id)
        {
            InitializeComponent();

            if (id > 0)
            {
                lbPId.Text = id.ToString();
                Text = "编辑权限";
                var p = _sysPrivilege.GetPrivilege(id);
                if (p != null)
                {
                    txtPrivilegeName.Text = p.Obj.Privilegename;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var pri = new Privilege
                {
                    Privilegeid = Converts.ConvertToInt(lbPId.Text),
                    Privilegename = this.txtPrivilegeName.Text
                };

                ResultInfo<int> id = _sysPrivilege.CreateUpdatePrivilege(pri);
                if (id.ResultNumber == ResultEnum.Success)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.Imformation_DoSuccess);
                    DialogResult = DialogResult.OK;

                }
                else
                {
                    MyMessageBox.ShowMessageBoxInfo(id.Describe);

                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxInfo(Resources.Imformation_SaveFailed);

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

    }
}
